package cn.com.blogonline;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jspsmart.upload.SmartUpload;


public class SaveLogoAction extends HttpServlet {
    
	private ServletConfig config;
	
	public void init(ServletConfig config) throws ServletException {
		this.config = config;
    }
    
    /*
     *  处理<GET> 请求方法.
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        /*
         * 上传文件
         */
		String imageName = null;
		SmartUpload mySmartUpload = new SmartUpload();
		try {
			// 初始化
			mySmartUpload.initialize(config,request,response);
			// 上载
			mySmartUpload.upload();
			// 保存上载文件到指定目录
			mySmartUpload.save("\\images");
			imageName = mySmartUpload.getFiles().getFile(0).getFileName();

		} catch (Exception e){
			System.out.println(e);
		}
		
       /*
        * 保存信息
        */
		HttpSession session = request.getSession(true);
		Blog blog = (Blog)session.getAttribute(Constants.LOGIN_USER_KEY);
		blog.setImage(imageName);
	    
		DbOperate	db=new DbOperate();
		db.update(blog);
		session.setAttribute(Constants.MODIFY_OK_KEY,new Integer(1));
		response.sendRedirect("/BlogOnline/adminLogo.jsp");
    }

	protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		// TODO 自动生成方法存根
		this.doGet(arg0,arg1);
	}
    
    
}
